package com.wisewin.pose.service;

import com.wisewin.pose.api.controller.WechatController;
import com.wisewin.pose.config.dto.ResultDTO;
import com.wisewin.pose.config.dto.ResultDTOBuilder;
import com.wisewin.pose.dao.*;
import com.wisewin.pose.entity.db.*;
import java.util.*;
import com.wisewin.pose.entity.dto.UserPrescriptionTitleDTO;
import com.wisewin.pose.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

@Service
@Transactional
public class UserPrescriptionService {
    final static Logger log = LoggerFactory.getLogger(WechatController.class);

    @Autowired
    private UserPhysicalRecordDAO userPhysicalRecordDAO;
    @Autowired
    private UserPhysicalHealthDAO userPhysicalHealthDAO;
    @Autowired
    private ActionDAO actionDAO;
    @Autowired
    private UserPrescriptionActionDAO userPrescriptionActionDAO;
    @Autowired
    private UserPrescriptionDAO userPrescriptionDAO;

    public ResultDTO queryDetail(Integer recordId){
        if(recordId == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询方案是否存在
        UserPrescriptionDO userPrescriptionDO = userPrescriptionDAO.queryByRecordId(recordId);
        if(userPrescriptionDO == null){
            return ResultDTOBuilder.failure("1", "当前没有生成方案", null);
        }
        // 查询方案详情
        List<UserPrescriptionTitleDTO> lstTitle = userPrescriptionActionDAO.queryTitle(userPrescriptionDO.getId());
        List<UserPrescriptionActionDO> lstAction = userPrescriptionActionDAO.queryList(userPrescriptionDO.getId());
        if(CollectionUtils.isEmpty(lstTitle) || CollectionUtils.isEmpty(lstAction)){
            return ResultDTOBuilder.failure("1", "查询数据有误", null);
        }
        // 处理数据
        for(UserPrescriptionTitleDTO title : lstTitle){
            List<UserPrescriptionActionDO> lstActionCurr = new ArrayList<>();
            for(UserPrescriptionActionDO action : lstAction){
                // 处理时间
                Integer duration = Integer.parseInt(action.getDuration());
                String durationVal = DateUtil.changeTimeFormat(Long.parseLong((duration / 1000)+""));
                action.setDuractionName(durationVal);
                // 添加数据
                if(title.getTitleDay().equals(action.getTitleDay())){
                    lstActionCurr.add(action);
                }
            }
            title.setLstAction(lstActionCurr);
        }
        // 查询风险信息
        List<UserPhysicalHealthDO> lstHealth = userPhysicalHealthDAO.queryList(recordId);
        // 返回数据
        Map<String, Object> result = new HashMap<>();
        result.put("lstHealth", lstHealth);
        result.put("lstPrescription", lstTitle);
        return ResultDTOBuilder.success(result);
    }

    public void dealUserPrescription(Integer recordId){
        // 查询报告是否存在
        UserPhysicalRecordDO userPhysicalRecordDO = userPhysicalRecordDAO.queryByRecordId(recordId);
        if(userPhysicalRecordDO == null){
            log.info("==========生成方案当前报告不存在 报告ID：" + recordId + "===========");
        }
        if(userPhysicalRecordDO.getIsCreate() == 1){
            log.info("==========生成方案当前报告已生成 报告ID：" + recordId + "===========");
        }
        // 查询风险项
        List<UserPhysicalHealthDO> lstPhysicalRisk = userPhysicalHealthDAO.queryPhysicalRiskList(recordId);
        if(CollectionUtils.isEmpty(lstPhysicalRisk)){
            log.info("==========生成方案当前报告不存在风险项 报告ID：" + recordId + "===========");
        }
        // 添加方案
        UserPrescriptionDO userPrescriptionDO = new UserPrescriptionDO();
        userPrescriptionDO.setPrescriptionName("青少年体态改善方案");
        userPrescriptionDO.setUserId(userPhysicalRecordDO.getUserId());
        userPrescriptionDO.setOrgId(userPhysicalRecordDO.getOrgId());
        userPrescriptionDO.setRecordId(userPhysicalRecordDO.getRecordId());
        userPrescriptionDAO.insert(userPrescriptionDO);
        // 体态评估风险项
        List<Integer> lstRiskId = new ArrayList<>();
        for(UserPhysicalHealthDO userPhysicalRiskDO : lstPhysicalRisk){
            lstRiskId.add(userPhysicalRiskDO.getExplainId());
        }
        // 获取动作
        List<UserPrescriptionActionDO> lstAction = new ArrayList<>();
        // 保存上次动作id
        List<Integer> lstActionLast2 = new ArrayList<>();
        List<Integer> lstActionLast3 = new ArrayList<>();
        // 处理数据
        for(int i = 0; i < 7; i++){
            String dayTitle = lstDayTitle[i];
            // 查询风险项训练动作
            Map<String, Object> map = new HashMap<>();
            map.put("riskId", StringUtils.join(lstRiskId, "|"));
            map.put("lstActionLast2", CollectionUtils.isEmpty(lstActionLast2) ? null : lstActionLast2);
            map.put("lstActionLast3", CollectionUtils.isEmpty(lstActionLast3) ? null : lstActionLast3);
            // 查询适用的动作
            List<ActionDO> lstActionSuit = actionDAO.queryActionListSuit(map);
            // 清空上次记录
            lstActionLast2 = new ArrayList<>();
            lstActionLast3 = new ArrayList<>();
            // 处理数据
            if(!CollectionUtils.isEmpty(lstActionSuit)){
                for(ActionDO actionDO : lstActionSuit){
                    UserPrescriptionActionDO userPrescriptionActionDO = new UserPrescriptionActionDO();
                    userPrescriptionActionDO.setUserPrescriptionId(userPrescriptionDO.getId());
                    userPrescriptionActionDO.setTitle(dayTitle);
                    userPrescriptionActionDO.setTitleDay(i + 1);
                    userPrescriptionActionDO.setActionId(actionDO.getId());
                    userPrescriptionActionDO.setTrainNum(actionDO.getId() == 1913 ? 3 : 10);
                    lstAction.add(userPrescriptionActionDO);
                    // 记录上一天动作
                    if(actionDO.getTrainType().equals("2")){
                        lstActionLast2.add(actionDO.getId());
                    }else if(actionDO.getTrainType().equals(3)){
                        lstActionLast3.add(actionDO.getId());
                    }
                }
            }
        }
        // 添加数据
        if(!CollectionUtils.isEmpty(lstAction)){
            userPrescriptionActionDAO.insertBatch(lstAction);
            // 修改报告是否生成方案
            userPhysicalRecordDO.setPrescriptionId(userPrescriptionDO.getId());
            userPhysicalRecordDAO.updateIsCreate(userPhysicalRecordDO);
        }
    }

    // 日期汉字信息
    public String[] lstDayTitle = new String []{"第一天", "第二天", "第三天", "第四天", "第五天", "第六天", "第七天", "第八天", "第九天", "第十天", "第十一天", "第十二天",
            "第十三天", "第十四天", "第十五天", "第十六天", "第十七天", "第十八天", "第十九天", "第二十天", "第二十一天", "第二十二天", "第二十三天", "第二十四天",
            "第二十五天", "第二十六天", "第二十七天", "第二十八天", "第二十九天", "第三十天"};
}
